'use strict';

/**
 * @ngdoc function
 * @name magisterkaGuiApp.controller:WatchEventCtrl
 * @description
 * # WatchEventCtrl
 * Controller of the magisterkaGuiApp
 */
angular.module('magisterkaGuiApp')
    .controller('WatchEventCtrl', function ($scope, $stateParams, $q, $log, $interval, $http, $timeout,$rootScope) {
        var eventResponseTimeoutPromise;



        if ($stateParams.token) {
            $scope.isTokenFromUrl = true;
        }

        var eventResponseServer = function (token) {
            return $http.post(BASE_WEBSERVICE_URL + '/facebook/get_current_event_state', {token: token});
        }

        var eventResponseDelegate = eventResponseServer;

        function createTimeLeftString(dateDiff) {
            return dateDiff.days + ' days ' + dateDiff.hours + ' hours ' + dateDiff.minutes + ' minutes ' + parseInt(dateDiff.seconds) + ' seconds';
        }


        if ($scope.isTokenFromUrl) {
            fetchEventState();
        }

        function fetchEventState() {
            eventResponseDelegate($stateParams.token).then(eventResponseHandler, function (data) {
                $log.error('Error during getting facebook event', data);
                $scope.errorOccurred = true;
            });
        }
        function eventResponseHandler(response) {

            $log.info('watch event response', response);
            $scope.event = response.data;


            $scope.event.dateCreated = new Date($scope.event.dateCreated);
            $scope.event.dateToBeFired = new Date($scope.event.dateToBeFired);

            var currentTime = new Date();
            var eventTime = $scope.event.dateToBeFired;
            var millisecondsPerHour = 1000 * 60 * 60;
            var dateDiff = getDateDiff(currentTime, eventTime);
            $scope.event.timeLeft =createTimeLeftString(dateDiff) ;

            function getDateDiff(date1, date2) {
                var millisBetween = date2.getTime() - date1.getTime();

                // get total seconds between the times
                var delta = millisBetween / 1000;

                var days = Math.floor(delta / 86400);
                delta -= days * 86400;
                var hours = Math.floor(delta / 3600) % 24;
                delta -= hours * 3600;
                var minutes = Math.floor(delta / 60) % 60;
                delta -= minutes * 60;
                var seconds = delta % 60;

                return {
                    days: days,
                    hours: hours,
                    minutes: minutes,
                    seconds: seconds
                };
            }

            if ($scope.event.finished) {
                $log.info('Event has finished. stopping getting info');
            } else {
                eventResponseTimeoutPromise = $timeout(function () {
                    fetchEventState();
                }, 500);
            }
        }
        $scope.stopGettingData = function () {
            if(eventResponseTimeoutPromise){
                $timeout.cancel(eventResponseTimeoutPromise);
            }
        }

        $scope.dimValue = function(val,min,max){
            if(!val)return min;

            if(val>max)return max;
            if(val<min) return min;
            return val;
        }
    });
